package binarysearch.test441;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/13 12:47
 */
public class Solution3 {

    public int arrangeCoins(int n) {
        long low = 1;
        long high = n;
        while (low <= high) {
            long mid = low + (high - low) / 2;   // 中位数
            long sum = mid * (mid + 1) / 2;      // 前n项和
            if (sum > n)
                high = mid - 1;
            else if (sum == n)
                return (int) mid;
            else if (sum < n)
                low = mid + 1;
        }
        return (int) (low - 1);
    }

}
